<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1000px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="作物类型">
							<el-input v-model="state.ruleForm.type" placeholder="请输入作物类型" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="病虫害名称">
							<el-input v-model="state.ruleForm.diseaseName" placeholder="请输入病虫害名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="省份编码">
							<el-input v-model="state.ruleForm.provinceCode" placeholder="请输入作物类型" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="省份">
							<el-input v-model="state.ruleForm.province" placeholder="请输入省份" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="发生面积">
							<el-input v-model="state.ruleForm.areaHappen" placeholder="请输入发生面积（万亩次）" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="防治面积">
							<el-input v-model="state.ruleForm.areaControl" placeholder="请输入防治面积（万亩次）" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="挽回损失">
							<el-input v-model="state.ruleForm.lossReturn" placeholder="请输入挽回损失（吨）" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实际损失">
							<el-input v-model="state.ruleForm.lossActual" placeholder="请输入实际损失（吨）" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="年份">
							<el-input v-model="state.ruleForm.year" placeholder="请输入年份" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemUserDialog">
import {ElMessage } from 'element-plus';
import axios from 'axios';
import { startCase } from 'lodash';
import api from "/@/api.js";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		areaControl: '',
		areaHappen : '',
		diseaseName: '',
		lossActual : '', 
		lossReturn : '',
		ppestId:'',
		province:'',
		provinceCode:'',
		status:true,
		type:'',
		year:'',
	},
	//deptData: [], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type;
	//console.log(row)
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改数据';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增数据';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	userDialogFormRef.value.resetFields();
		// });
		state.ruleForm = {
            areaControl: '',
			areaHappen : '',
			diseaseName: '',
			lossActual : '', 
			lossReturn : '',
			ppestId:'',
			province:'',
			provinceCode:'',
			status:true,
			type:'',
			year:'',
		}
	}
	state.dialog.isShowDialog = true;
	//getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if(state.dialog.type === 'add'){
		if(state.ruleForm.status){
			state.ruleForm.status = '1';
		}else{
			state.ruleForm.status = '0';
		}
		// 新增
		axios({
		method: 'post',
		url: api.commonUrl + '/provincepest/addProvincePest',
		data:state.ruleForm,
	}).then(function (response) {
		if(response.data.code == 200){
			var user = response.data.data
			ElMessage.success(response.data.message);
		}else{
			ElMessage.error(response.data.message);
			
		}
		emit('refresh');
	});
	}else{
		if(state.ruleForm.status){
			state.ruleForm.status = '1';
		}else{
			state.ruleForm.status = '0';
		}
		// 修改
		axios({
		method: 'post',
		url: api.commonUrl + '/provincepest/updateprovincepest',
		data:state.ruleForm,
	}).then(function (response) {
		if(response.data.code == 200){
			var user = response.data.data
			ElMessage.success(response.data.message);
		}else{
			ElMessage.error(response.data.message);
		}
		emit('refresh');
	});
	}
	
	closeDialog();
};


// 暴露变量
defineExpose({
	openDialog,
});
</script>
